package edu.ynu.se.xiecheng.achitectureclass.service;

import edu.ynu.se.xiecheng.achitectureclass.dao.GreenhouseDao;
import edu.ynu.se.xiecheng.achitectureclass.dto.DeviceDataWithGreenhouseDTO;
import edu.ynu.se.xiecheng.achitectureclass.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.entity.Greenhouse;
import edu.ynu.se.xiecheng.achitectureclass.entity.DeviceDataBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DeviceDataServiceImpl implements IDeviceDataService {
    
    @Autowired
    private EntityManager entityManager;
    
    @Autowired
    private IDeviceService deviceService;
    
    @Autowired
    private GreenhouseDao greenhouseDao;

    @Override
    public List<DeviceDataWithGreenhouseDTO> getAllSensorsData(Long greenhouseId) {
        List<DeviceDataBase> sensors = new ArrayList<>();
        Greenhouse greenhouse = greenhouseDao.findById(greenhouseId).orElse(null);

        if (greenhouse == null) {
            throw new RuntimeException("Greenhouse not found");
        }


        return sensors.stream().map(sensor -> {
            DeviceDataWithGreenhouseDTO dto = new DeviceDataWithGreenhouseDTO();
            // 设置传感器信息
            dto.setId(sensor.getId());
            dto.setTimestamp(sensor.getTimestamp());
            dto.setDataType(sensor.getDataType());
            dto.setName(sensor.getName());
            dto.setValue(sensor.getValue());
            dto.setBoxId(sensor.getBoxId());
            dto.setStatus(sensor.getStatus());
            dto.setConnState(sensor.getConnState());
            dto.setConnStateTimestamp(sensor.getConnStateTimestamp());
            dto.setDeviceId(sensor.getDeviceId());

            // 设置大棚信息
            dto.setGreenhouseId(greenhouse.getId());
            dto.setGreenhouseName(greenhouse.getName());
            dto.setGreenhouseAddress(greenhouse.getAddress());
            dto.setGreenhouseArea(greenhouse.getArea());
            dto.setGreenhouseBoxNo(greenhouse.getBoxNo());

            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public <T extends DeviceDataBase> T saveSensorData(T sensorData) {
        if (sensorData.getId() == null) {
            entityManager.persist(sensorData);
        } else {
            sensorData = entityManager.merge(sensorData);
        }
        return sensorData;
    }

    @Override
    public Boolean refreshData(Long greenhouseId) {
        try {
            // 获取大棚信息
            Greenhouse greenhouse = greenhouseDao.findById(greenhouseId).orElse(null);
            if (greenhouse == null) {
                return false;
            }

            // 获取设备列表
            List<Device> devices = deviceService.findByGreenhouseId(greenhouseId);
            if (devices.isEmpty()) {
                return false;
            }

            // 更新每个设备的数据
            for (Device device : devices) {
                // 这里可以添加具体的数据刷新逻辑
                // 例如调用设备API获取最新数据
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}